package com.yuan.farmerwork.ynblog.rest;

import com.yuan.farmerwork.ynblog.domain.pojo.OutPigeData;
import com.yuan.farmerwork.ynblog.domain.pojo.PigeonholeData;
import com.yuan.farmerwork.ynblog.service.BlogService;
import com.yuan.farmerwork.ynblog.utils.constants.CommConstants;
import com.yuan.farmerwork.ynblog.utils.constants.PageConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 归档页面前端控制器.
 *
 * @author yjs
 * @date 2021/1/14 15:32
 */
@Controller
public class PigeonholeController {

    @Autowired
    private BlogService blogService;

    /**
     * 归档页.
     *
     * @param map 页面参数map
     * @return
     */
    @GetMapping("/pigeonhole")
    public ModelAndView projects(Map map) {
        List<PigeonholeData> pigeonholeData = blogService.guiDangSum();
        Map<String, List<PigeonholeData>> rex = pigeonholeData.stream()
                .collect(Collectors.groupingBy(PigeonholeData::getYearMonth));
        List<OutPigeData> outPigeData = new ArrayList<>();
        Set<Map.Entry<String, List<PigeonholeData>>> entrySet = rex.entrySet();
        for (Map.Entry<String, List<PigeonholeData>> entry : entrySet) {
            OutPigeData od = new OutPigeData();
            String[] split = entry.getKey().split(CommConstants.SYMBOL_CONNECIVE);
            od.setYearMonth(Long.valueOf(split[0] + split[1]));
            od.setPigeonholeData(entry.getValue());
            outPigeData.add(od);
        }
        outPigeData = outPigeData.stream()
                .sorted(Comparator.comparing(OutPigeData::getYearMonth).reversed())
                .collect(Collectors.toList());
        map.put(PageConstants.PAGE_PARAM_PIGE_BLOGS, outPigeData);
        return new ModelAndView(PageConstants.PAGE_PIGEONHOLE, map);
    }
}
